package com.qianfeng.missfood.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.missfood.R;
import com.qianfeng.missfood.adapter.Category_Detail_ptrListView_adapter;
import com.qianfeng.missfood.category_bean.Category_Detail_Bean;
import com.qianfeng.missfood.category_bean.Category_Detail_Info;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class Category_Detail_ptrListView_Fragment extends Fragment implements PullToRefreshBase.OnRefreshListener2<ListView> {

    @ViewInject(R.id.details_ptrListView)
    private PullToRefreshListView detail_ptrListView;

    private String categoryId;
    private int page = 1;
    private HttpUtils httpUtils = new HttpUtils();
    private String detailUrl;
    private List<Category_Detail_Info> infos = new ArrayList<>();
    private Category_Detail_ptrListView_adapter ptrListView_adapter;
    private int sortType;
    public Category_Detail_ptrListView_Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category__detail_ptr_list_view_, container, false);
        ViewUtils.inject(this, view);
        categoryId = getArguments().getString("categoryId");
        sortType=getArguments().getInt("sortType");
        if(sortType==0){
            sortType=1;
        }
        initView();
        initData();
        return view;
    }


    private void initView() {
        detail_ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
        detail_ptrListView.setOnRefreshListener(this);
        ptrListView_adapter = new Category_Detail_ptrListView_adapter(infos, getActivity());
        detail_ptrListView.setAdapter(ptrListView_adapter);
    }

    private void initData() {

        detailUrl = "http://api.mishi.cn/index/goods/category/?_method=GET&p-pv=1.0&p-apiv=1.0&categoryId=" + categoryId + "&cityCode=0755&sortType="+sortType+"&pageSize=10&lng=113.945501&currentPage=" + page + "&lat=22.534877&city=%E6%B7%B1%E5%9C%B3%E5%B8%82%20HTTP/1.1";
        httpUtils.send(HttpRequest.HttpMethod.GET, detailUrl, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (responseInfo != null) {
                    String result = responseInfo.result;
                    Category_Detail_Bean bean = JSONObject.parseObject(result, Category_Detail_Bean.class);
                    if (bean != null && bean.getResultList() != null) {
                        infos.addAll(bean.getResultList());
                        ptrListView_adapter.notifyDataSetChanged();
                        detail_ptrListView.onRefreshComplete();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                detail_ptrListView.onRefreshComplete();
            }
        });


    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        infos.clear();
        page = 1;
        initData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        page++;
        initData();
    }
}
